import { Access, useAccess } from '@umijs/max';
import { ReactNode } from 'react';

type Props = {
  children: ReactNode;
  // 是否有权限
  accessible?: boolean;
  // 权限标志字符串
  perm?: string;
};

// accessible、perm 二选一传入组件判断是否有使用权限
const AccessPage: React.FC<Props> = ({ children, accessible = false, perm = '' }) => {
  const access = useAccess();
  let _accessible = access.canAdmin || accessible;
  if (typeof perm === 'string') {
    _accessible = _accessible || (access[perm] ?? false);
  }
  return <Access accessible={_accessible}>{children}</Access>;
};

export default AccessPage;
